﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using FinPlusInterfaces;
using FinPlusCompCore;

namespace FinPlusCompProxy
{
    public class QLEngineYieldCurves : FinPlusComponent
    {
        [State(Test = true)]
        public override bool IsValid { get; set; }

        private enum Params { CurveName, LinkedCurve };
        private readonly IFinPlusComp _yieldCurveBuildConn, _instrumentBuildConn, _instrumentConn;
        private readonly QLAnalytics _analytics;
        private readonly YieldCurves _yieldCurves;
        private readonly BuildEvent _buildEvent;

        //construct
        public QLEngineYieldCurves(IFinPlusComp analytics, IFinPlusComp yieldCurveBuilds, IFinPlusComp instrumentBuilds, IFinPlusComp instruments, BuildEvent buildEvents)
        {
            _analytics = (QLAnalytics)analytics;
            _yieldCurveBuildConn = yieldCurveBuilds;
            _instrumentBuildConn = instrumentBuilds;
            _instrumentConn = instruments;
            _buildEvent = buildEvents;
            _yieldCurves = new YieldCurves();
           
            AddCurves();

            IsValid = true;
        }

        //common control interface
        public override void Dispose()
        {
            if(_instrumentConn != null)
                _instrumentConn.Dispose(); 

            if(_yieldCurveBuildConn != null)
                _yieldCurveBuildConn.Dispose();

            if (_instrumentBuildConn != null)
                _instrumentBuildConn.Dispose(); 
        }

        //private
        private void AddCurves()
        {
            foreach (var yieldCurveBuild in _yieldCurveBuildConn.Adapter.Values)
            {
                var curveName = yieldCurveBuild[Params.CurveName.ToString()].ToString();
                _yieldCurves.TryAdd(curveName, new YieldCurve(_analytics, curveName, yieldCurveBuild, _instrumentBuildConn.Adapter, _instrumentConn.Adapter.ToList(Params.LinkedCurve.ToString(), curveName, ','), _buildEvent));
            }
        }
    }
}
